// 路由守卫
import router from './router'
import store from './store'
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  const hasUserId = sessionStorage.getItem('userId')
  if (hasUserId) {
    store.commit('SET_AVATAR', sessionStorage.getItem('userImg') || '')
    if (to.path === '/login') {
      // 如果已经登录,跳转到用户中心
      next({ path: '/user' })
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
